package com.ruoyi.oto.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.MemberUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.oto.domain.DemandsAnno;
import com.ruoyi.oto.domain.DemandsDepart;
import com.ruoyi.oto.domain.DemandsRecord;
import com.ruoyi.oto.domain.dto.*;
import com.ruoyi.oto.domain.vo.DemandsDepartDetailVO;
import com.ruoyi.oto.domain.vo.DemandsRecordDetailVO;
import com.ruoyi.oto.domain.vo.MemberInfoVO;
import com.ruoyi.oto.service.DemandsAnnoService;
import com.ruoyi.oto.service.DemandsDepartService;
import com.ruoyi.oto.service.DemandsRecordService;
import io.swagger.annotations.ApiOperation;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 小程序-诉求管理
 */
@RestController
@RequestMapping("/api/v2/demands")
public class DemandsApiController extends BaseController {

    @Autowired
    private DemandsRecordService demandsRecordService;

    @Autowired
    private DemandsDepartService demandsDepartService;

    @Autowired
    private DemandsAnnoService demandsAnnoService;
    /**
     * 企业提交诉求
     * @return
     */
    @PostMapping("saveDemandsForCompany")
    public AjaxResult<Boolean> saveDemandsForCompany( @Validated  @RequestBody DemandsCompanyDTO demandsCompanyDTO) {
        MemberUser member = this.getLoginUser().getMember();
        Boolean result = demandsRecordService.saveDemandsRecord(member,demandsCompanyDTO);
        return AjaxResult.success(result);
    }

    /**
     * 小程序获取诉求列表-分状态
     * @param demandsQuryDTO
     * @return
     */
    @PostMapping ("listDemands")
    public AjaxResult<IPage<DemandsRecord>> listDemands(@Validated @RequestBody  DemandsQuryDTO demandsQuryDTO){
        MemberUser member = this.getLoginUser().getMember();
        IPage<DemandsRecord> result  = demandsRecordService.listDemandsFront(member,demandsQuryDTO);
        return AjaxResult.success(result);
    }

    /**
     * 小程序获取要审核的和审核失败的诉求详情
     * @param demandId
     * @return
     */
    @GetMapping("demandsCheckDetail/{demandId}")
    public AjaxResult<DemandsRecord> demandsCheckDetail(@PathVariable("demandId") Long demandId){
        DemandsRecord record = demandsRecordService.getById(demandId);
        return AjaxResult.success(record);
    }

    /**
     * 小程序获取已经审核的诉求详情
     * @param demandId
     * @return
     */
    @GetMapping("demandsDetail/{demandId}")
    public AjaxResult<DemandsRecordDetailVO> demandsDetail(@PathVariable("demandId") Long demandId){
        DemandsRecordDetailVO record = demandsRecordService.demandsDetail(this.getLoginUser(),demandId);
        return AjaxResult.success(record);
    }

    /**
     * 联企人员审核诉求
     * @param checkDemandsDTO
     * @return
     */
    @PostMapping ("checkDemands")
    public AjaxResult<Long> checkDemands(@Validated @RequestBody CheckDemandsDTO checkDemandsDTO){
        Long demandsId = demandsRecordService.checkDemands(this.getLoginUser(),checkDemandsDTO);
        return AjaxResult.success(demandsId);
    }

    /**
     * 新增批注,对诉求分到的部门,每个部门的答复记录 打批注
     * @return
     */
    @PostMapping("/addAnno")
    public AjaxResult<?> addAnnotation(@Validated @RequestBody AddAnnotationDTO addAnnotationDTO){
        if(ObjectUtil.isNull(addAnnotationDTO.getDmdDptId())){
            throw new ServiceException("部门诉求ID和回复ID不能同时为空");
        }
        demandsAnnoService.saveDemandsAnno(this.getLoginUser().getMember(),addAnnotationDTO);
        return AjaxResult.success();
    }

    /**
     * 删除批注
     * @return
     */
    @GetMapping("/deleteAnno/{id}")
    public AjaxResult<?> deleteAnno(@PathVariable Long id){
        demandsAnnoService.deleteDemandsAnno(this.getLoginUser().getMember(),id);
        return AjaxResult.success();
    }

    /**
     * 获取批注记录
     * @param demandId  诉求Id
     * @param refId     关联的ID 诉求部门ID或者回复ID
     * @return
     */
    @GetMapping("/listAnno/{demandId}/{refId}")
    public AjaxResult<List<DemandsAnno>> listAnnotation(@PathVariable Long demandId,@PathVariable Long refId){
        List<DemandsAnno> demandsAnnos = demandsAnnoService.listAnnotation(this.getLoginUser().getMember(), demandId, refId);
        return AjaxResult.success(demandsAnnos);
    }

}
